package com.bigstonecontainer.training.springboot.frm.server.sys.service.impl;

import com.bigstonecontainer.training.springboot.frm.server.core.cache.CoreCache;
import com.bigstonecontainer.training.springboot.frm.server.core.domain.LoginUser;
import com.bigstonecontainer.training.springboot.frm.server.sys.domain.SysMenu;
import com.bigstonecontainer.training.springboot.frm.server.sys.mapper.SysMenuMapper;
import com.bigstonecontainer.training.springboot.frm.server.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> getCurrentUserMenuList(String token) {
        LoginUser loginUser = CoreCache.USERS.get(token);
        if (loginUser == null || !StringUtils.hasText(loginUser.getRoleIds())) {
            return new ArrayList<>();
        }
        String[] roles = loginUser.getRoleIds().split(",");
        if (Arrays.asList(roles).contains("1")) {
            return sysMenuMapper.selectMenuList();
        }
        return sysMenuMapper.selectMenuListByRoleIds(loginUser.getRoleIds());
    }
}
